package com.qf.drtest;

import com.qf.drtest.utils.ThreadPoolUtils;

import java.util.concurrent.CountDownLatch;

public class MainTest2 {
    public static void main(String[] args) throws InterruptedException {
        //门闩对象
        CountDownLatch cdl = new CountDownLatch(2);

        long start = System.currentTimeMillis();

        ThreadPoolUtils.tp2().submit(()->{
            //煮饭
            try {
                Thread.sleep(2000);

                System.out.println("煮饭...." + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                //解开门闩
                cdl.countDown();
            }
        });

        ThreadPoolUtils.tp().submit(()->{
            //炒菜
            try {
                Thread.sleep(1000);

                System.out.println("炒菜...." + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                //解开门闩
                cdl.countDown();
            }
        });

        //等待我们初始化的2个门闩都解开后，再继续向下执行代码
        cdl.await();
        //这个开吃的代码应该等待煮饭和炒菜都完成后，才执行（线程间通讯）
        long end = System.currentTimeMillis();
        //开吃
        System.out.println("开吃.... 耗时：" + (end-start) + Thread.currentThread().getName()) ;
    }
}
